/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-6-27   22:29
* filename: LaborD3D10Device.cpp
-----------------------------------------------------------------------------
*/

#include "LaborD3D10Device.h"
NAMESPACE_LABOR3D_BEGIN
//---------------------------------------------------------------------
CLD3D10Device::eExceptionsErrorLevel CLD3D10Device::mExceptionsErrorLevel = CLD3D10Device::D3D_NO_EXCEPTION;

//---------------------------------------------------------------------
CLD3D10Device::CLD3D10Device( ID3D10Device * D3D10device ) : m_pD3D10Device(D3D10device)
{

}
//---------------------------------------------------------------------
CLD3D10Device::CLD3D10Device() : m_pD3D10Device(0)
{

}

//---------------------------------------------------------------------
CLD3D10Device::~CLD3D10Device()
{

}

//---------------------------------------------------------------------
ID3D10Device * CLD3D10Device::operator=( ID3D10Device * D3D10device )
{
	m_pD3D10Device = D3D10device; 
	return m_pD3D10Device;
}
//---------------------------------------------------------------------
const bool CLD3D10Device::isNull()
{
	return m_pD3D10Device == 0;
}
//---------------------------------------------------------------------
const string CLD3D10Device::getErrorDescription(const HRESULT lastResult /* = NO_ERROR */) const
{
	if (!m_pD3D10Device)
	{
		return "NULL device";
	}

	if (D3D_NO_EXCEPTION == mExceptionsErrorLevel)
	{
		return "infoQ exceptions are turned off";
	}

	string res;

	switch (lastResult)
	{
	case NO_ERROR:
		break;
	case E_INVALIDARG:
		res = res + "invalid parameters were passed.\n";
		break;
	default:
		assert(false); // unknown HRESULT
	}

	ID3D10InfoQueue * pInfoQueue = NULL; 
	HRESULT hr = m_pD3D10Device->QueryInterface(__uuidof(ID3D10InfoQueue), (LPVOID*)&pInfoQueue);

	if (SUCCEEDED(hr))
	{
		unsigned int numStoredMessages = pInfoQueue->GetNumStoredMessages();
		for (unsigned int i = 0 ; i < numStoredMessages ; i++ )
		{
			// Get the size of the message
			SIZE_T messageLength = 0;
			hr = pInfoQueue->GetMessage(i, NULL, &messageLength);
			// Allocate space and get the message
			D3D10_MESSAGE * pMessage = (D3D10_MESSAGE*)malloc(messageLength);
			hr = pInfoQueue->GetMessage(i, pMessage, &messageLength);
			res = res + pMessage->pDescription + "\n";
			free(pMessage);
		}
	}

	return res;
}
//---------------------------------------------------------------------
void CLD3D10Device::release()
{
	SafeRelease( m_pD3D10Device );
}
//---------------------------------------------------------------------
ID3D10Device * CLD3D10Device::get()
{
	return m_pD3D10Device;
}
//---------------------------------------------------------------------
void CLD3D10Device::setExceptionsErrorLevel( const eExceptionsErrorLevel exceptionsErrorLevel )
{
	mExceptionsErrorLevel = exceptionsErrorLevel;
}
//---------------------------------------------------------------------
const CLD3D10Device::eExceptionsErrorLevel CLD3D10Device::getExceptionsErrorLevel()
{
	return mExceptionsErrorLevel;
}
//---------------------------------------------------------------------

NAMESPACE_LABOR3D_END